#!/bin/sh
# busybox_static
# Builds from https://github.com/puppylinux-woof-CE/petbuilds

### LINKS
## http://ewontfix.com/12/
## https://github.com/kanj/elfs/issues/4
## http://wiki.musl-libc.org/wiki/Building_Busybox

if [ -z ${MWD} ] ; then
	echo "Use build.sh"
	exit 1
fi

[ -z $STATIC ] && STATIC=1

. ../../func

URL=http://busybox.net/downloads
PKG=busybox_static
SRC=busybox
VER=1.28.2
COMP=tar.bz2
DESC="BusyBox - The Swiss Army Knife of Embedded Linux - statically built"
DEPS=
CAT=BuildingBlock

[ -z ${DOTconfig} ] && DOTconfig="DOTconfig"
[ "$CREATE_PET" ]   && DOTconfig="DOTconfig_full"


build() {
	cd ${SRC}-${VER}
	# diff -Naur busybox-1.24.2 busybox-1.24.2_patched > busybox.patch
	for p in $(ls ../patches/*.patch 2>/dev/null | sort) ; do
		patch -p1 < $p
	done
	[ -f ../${DOTconfig} ] && cp -fv ../${DOTconfig} .config
	#-
	BLACKLIST=$(
		echo CONFIG_FEATURE_DEVFS '-> tells busybox to look for names like /dev/loop/0 instead of /dev/loop/0'
		echo CONFIG_DEVFSD '(obsolete)'
	)
	#-
	if [ "$STATIC" = "1" ] ; then
		# initrd
		ENABLE_KONFIG=$(
			echo CONFIG_FEATURE_PREFER_APPLETS
			echo CONFIG_STATIC
		)
		DISABLE_KONFIG=$(
			echo CONFIG_ASH_JOB_CONTROL
			echo FEATURE_VI_REGEX_SEARCH '(needs GNU regexp extensions)'
			echo FEATURE_MOUNT_NFS '(needs rpc/rpc.h)'
			echo FEATURE_INETD_RPC '(needs rpc/rpc.h)'
			echo CONFIG_GREP 'not supported by musl'
			echo CONFIG_EGREP 'not supported by musl'
			echo CONFIG_FGREP 'not supported by musl'
		)
	else # main sfs
		ENABLE_KONFIG=$(
			echo CONFIG_ASH_JOB_CONTROL
		)
		DISABLE_KONFIG=$(
			echo CONFIG_FEATURE_PREFER_APPLETS
			echo CONFIG_STATIC
		)
	fi
	#-
	BLACKLIST="$(echo "$BLACKLIST" | sed -e 's| .*||')"
	ENABLE_KONFIG="$(echo "$ENABLE_KONFIG" | sed -e 's| .*||')"
	DISABLE_KONFIG="$(echo "$DISABLE_KONFIG" | sed -e 's| .*||')"
	#echo "BL: $BLACKLIST"
	#echo "EN: $ENABLE_KONFIG"
	#echo "DI: $DISABLE_KONFIG"
	#-
	for KONFIG in $ENABLE_KONFIG
	do
		sed -i "s|.*${KONFIG}.*|${KONFIG}=y|" .config
	done
	for KONFIG in $DISABLE_KONFIG $BLACKLIST
	do
		sed -i "s|.*${KONFIG}=.*|# ${KONFIG} is not set|" .config
	done
	#-
	[ -d "$XPATH" ] && CRC="CROSS_COMPILE=${XCOMPILER}-" #XCOMPILER is set in func
	_make ${MKFLAG} ${CRC} LDFLAGS="--static" install
	abort_if_file_not_found busybox
	#--
	if [ "$CREATE_PET" ] ; then
		A=$ARCH
		case $ARCH in i?86) A=x86 ;; esac
		mv _install ${SRC}_static-${VER}-${A}_musl
		dir2pet -x -s -n ${SRC}_static-${VER}-${A}_musl
		mkdir -p ${MWD}/00_${ARCH}/
		mv -fv ${SRC}_static-${VER}-${A}_musl.pet ${MWD}/00_${ARCH}/ || exit
		cp -v .config ../${DOTconfig}
		cd ..
		rm -rf ${SRC}-${VER}
		exit 0
	fi
	#--
	alist=$(find _install -type l | sed 's|.*/||' | sort | tr '\n' ' ')
	sed -i -e "s%applet_list='.*%applet_list='${alist}'%" ../bb-create-symlinks
	cp -v .config ../${DOTconfig}
	cd -
	install -d -m 0755 ./${PKG}-${VER}-${ARCH}/bin
	_install ${SRC}-${VER}/busybox ./${PKG}-${VER}-${ARCH}/bin
	install -m 0755 bb-create-symlinks ./${PKG}-${VER}-${ARCH}/bin
	install -m 0755 bb-delete-symlinks ./${PKG}-${VER}-${ARCH}/bin
}

package() {
	# add this recipe
	install -d -m 0755 ./${PKG}-${VER}-${ARCH}/build
	cat ${PKG}.petbuild > ./${PKG}-${VER}-${ARCH}/build/$PKG-build-recipe
	install -m 0644 DOTconfig ./${PKG}-${VER}-${ARCH}/build
	# move, don't package
	cp -a --remove-destination ./${PKG}-${VER}-${ARCH}/* ../../00_${ARCH}
	rm -r ${SRC}-${VER}
	rm -r ${PKG}-${VER}-$ARCH
	echo "moved to initrd"
	echo "done!"
}

# main
retrieve ${SRC}-${VER}.${COMP}
[ "$DLD_ONLY" = "yes" ] && exit
extract ${SRC}-${VER}.${COMP}
build
package
